package com.neueedu.android.ticket;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.neueedu.android.R;
import com.neueedu.android.entity.Ticket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class TicketActivity3 extends AppCompatActivity {


    private TextView tvAddConcat;
    private ListView selectConcat;
    SelectConcatAdapter adapter ;

    ArrayList<Map<String, Object>> selectConcatList = new ArrayList<>();


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket3);
//        Intent intent = getIntent();
//        Ticket ticket = (Ticket) intent.getSerializableExtra("ticket");

        tvAddConcat = findViewById(R.id.tvAddConcat);
        selectConcat = findViewById(R.id.selectConcat);

        tvAddConcat.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TicketActivity3.this,TicketActivity4.class);
                startActivityForResult(intent,1);
            }
        });


        adapter = new SelectConcatAdapter(selectConcatList);
        selectConcat.setAdapter(adapter);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            selectConcatList.clear();

            ArrayList<Map<String, Object>> selectConcatListExt = (ArrayList<Map<String, Object>>) data.getSerializableExtra("selectConcatList");
            selectConcatList.addAll(selectConcatListExt);
            adapter.notifyDataSetChanged();
        }
    }


    class SelectConcatAdapter extends BaseAdapter{

        List<Map<String, Object>> concatList ;


        public SelectConcatAdapter(List<Map<String, Object>> concatList) {
            this.concatList = concatList;
        }

        @Override
        public int getCount() {
            return concatList.size();
        }

        @Override
        public Object getItem(int position) {
            return concatList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v;
            ViewHolder viewHolder;
            final Map data = (Map) getItem(position);
            if(convertView == null){
                v = getLayoutInflater().inflate(R.layout.activity_ticket3_item,null);
                viewHolder = new  ViewHolder();

                viewHolder.ivDelete = v.findViewById(R.id.ivDelete);
                viewHolder.tvName = v.findViewById(R.id.tvName);
                viewHolder.tvIdNo = v.findViewById(R.id.tvIdNo);
                viewHolder.tvType = v.findViewById(R.id.tvType);

                v.setTag(viewHolder);
            }else{
                v=  convertView;
                viewHolder = (ViewHolder) v.getTag();
            }

            //viewHolder
            viewHolder.ivDelete.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

                    //移除当前行

                    Dialog dialog = new AlertDialog.Builder(TicketActivity3.this)
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setTitle("是否删除").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            concatList.remove(position);
                            adapter.notifyDataSetChanged();
                        }
                    }).setNegativeButton("取消",null).show();




                }
            });

            viewHolder.tvName.setText((String) data.get("name"));
            viewHolder.tvIdNo.setText((String) data.get("idno"));
            viewHolder.tvType.setText((String) data.get("type"));

            return v;
        }

        class ViewHolder {
            ImageView ivDelete;
            TextView tvName;
            TextView tvIdNo;
            TextView tvType;

        }
    }


//    //删除某一个联系人
//    private void removeConcat(int position) {
//
//
//
//
//    }


}
